(function() {
	var role = require("./role");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var AdminCon = require("../../core/admin/_con");
	var BusCon = require("../../core/bus/_con");
	var DotnarCaseCon = require("../../core/dotnar_case/_con");
	var ResponObj = require("../_include/responObj");
	module.exports = {
		prefix: "/admin",
		get: {
			"/": function() {
				try {
					var admin = role.getAdmin(this);
					this.body = admin;
				} catch (errorMsg) {
					this.body = ResponObj("error", errorMsg);
				}
			},
			"/scanUser/:id_or_name": require("./user.rest.js").get["/adminScan/:id_or_name"],
			//管理员退出登录
			"/loginout": function() {
				try {
					var admin = role.getAdmin(this);
					//删除loginHash中同管理员名的信息
					db.remove_list(config.dbname.loginHash, {
						admin_id: admin._id
					});
					//清空Cookie
					this.cookies.set("loginSession", "");
					this.body = ResponObj("json", "管理员下线成功");
				} catch (errorMsg) {
					this.body = ResponObj("error", errorMsg);
				}
			},
		},
		post: {
			"/register": function() {
				// //权限校验
				// if (!role.resCheckRole("admin", this)) {
				// 	return
				// }

				var admin_info = this.request.body;
				//先验证验证码
				if (!(admin_info.password && admin_info.confirm_password)) {
					throwCusError("表单不完整");
				} else if (admin_info.password !== admin_info.confirm_password) {
					throwCusError("两次密码输入不匹配");
				} else {
					var admin = new AdminCon(admin_info);
					this.body = ResponObj("json", admin);
				}
			},
			//管理员登录
			//.body:admin_info
			"/login": function() {
				var form_data = this.request.body;
				var admin = AdminCon.login(form_data);
				//删除loginHash中同管理员名的信息
				db.remove_list(config.dbname.loginHash, {
					admin_id: admin._id
				});
				//注入新的Session
				var hash = admin._id.substr(2) + (+new Date).toString(16);
				this.cookies.set("admin_loginSession", hash, {
					expires: new Date((+new Date) + config.autoSignUpTime)
				});
				db.insert(config.dbname.loginHash, {
					_id: hash,
					admin_id: admin._id,
					type: "admin",
					beginTime: +new Date
				});
				this.body = ResponObj("json", admin);
			},
			"/add_case": require("./case.rest").post["/add_case"],
			"/add_news": require("./news.rest").post["/add_news"],
			"/add_bus": function() {
				var admin = role.getAdmin(this);
				var body = this.request.body;
				var bus_name = body.name.trim();
				if (!(body.password && body.confirm_password && body.domain)) {
					throwCusError("表单不完整");
				} else if (body.password !== body.confirm_password) {
					throwCusError("两次密码输入不匹配");
				} else {
					delete body.confirm_password;
					//（管理员模式进行）账号注册到数据库中
					var bus = new BusCon(body, true);
					var domain = body.domain;
					bus.initDomain(domain, body.is_secondary_domain);
					this.body = ResponObj("json", BusCon.get(bus._id));
				}
			},
		},
		put: {
			"/update_case": require("./case.rest").put["/update_case"],
			"/update_news": require("./news.rest").put["/update_news"],
			"/reply_message": require("./message.rest").put["/reply_message"],
		},
		"delete": {
			"/remove_case": require("./case.rest")["delete"]["/remove_case"],
			"/remove_news": require("./news.rest")["delete"]["/remove_news"],
			"/remove_message": require("./message.rest")["delete"]["/remove_message"],
		}
	}
}());